package com.rongzhe.house.presenter;

import android.content.Intent;
import android.view.View;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.AuthenticationInfoTo;
import com.rongzhe.house.entity.voo.HouseDetail;
import com.rongzhe.house.entity.voo.UserAuthInfo;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.manager.HouseManager;
import com.rongzhe.house.manager.OrderManager;
import com.rongzhe.house.manager.UserManager;
import com.rongzhe.house.ui.UiControlInterface;
import com.rongzhe.house.ui.activity.ContractActivity;
import com.rongzhe.house.ui.activity.HouseInfoActivity;
import com.rongzhe.house.ui.activity.LookForHouseActivity;
import com.rongzhe.house.ui.activity.PayTypeActivity;
import com.rongzhe.house.ui.activity.UserAuthActivity;
import com.rongzhe.house.utils.UIUtils;

/**
 * Created by huang on 2017/11/19.
 */

public class HouseInfoPresenter extends BasePresenter {
    private int mHouseId;
    private HouseDetail mHouseDetail;
    public HouseInfoPresenter(UiControlInterface uiControlInterface) {
        super(uiControlInterface);
    }

    @Override
    public void start(Intent intent) {
        mHouseId = intent.getIntExtra(HouseInfoActivity.EXTRA_HOUSE_ID, -1);
        if (mHouseId == -1) {
            getUiControlInterface().finish();
            return;
        }
        getUiControlInterface().showProgress(getString(R.string.in_process));
        HouseManager.getInstance().getHouseDetail(mHouseId, new DataListener<HouseDetail>() {
            @Override
            public void onSuccess(HouseDetail data, String message) {
                mHouseDetail = data;
                ((HouseInfoActivity)getUiControlInterface()).setData(data);
            }

            @Override
            public void onFailed(Throwable error, String message) {
                getUiControlInterface().showToast(message);
            }

            @Override
            public void onEnd() {
                getUiControlInterface().dismissProgress();
            }
        });
    }

    public void changeStarState() {
        if (!UserManager.getInstance().isLogin()) {
            getUiControlInterface().launchActivity(true, null);
            return;
        }
        HouseManager.getInstance().updateHouseStarState(mHouseId, new DataListener<Object>() {
            @Override
            public void onSuccess(Object data, String message) {
                ((HouseInfoActivity)getUiControlInterface()).onStarChanged();
            }

            @Override
            public void onFailed(Throwable error, String message) {

            }

            @Override
            public void onEnd() {

            }
        });
    }

    public void onLookingHouseClick() {
        Intent intent = new Intent(mContext, LookForHouseActivity.class);
        intent.putExtra(LookForHousePresenter.EXTRA_HOUSE_TITLE, mHouseDetail.getName());
        intent.putExtra(LookForHousePresenter.EXTRA_HOUSE_ID, mHouseId);
        getUiControlInterface().launchActivity(true, intent);
    }

    private void showUserAuthHintDialog(boolean faild) {
        getUiControlInterface().showAlert(UIUtils.createTwoButtonDialog(mContext,
                getString(R.string.hint),
                faild ? getString(R.string.auth_fail_hint) : getString(R.string.need_auth_hint),
                getString(android.R.string.cancel),
                getString(android.R.string.ok),null, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, UserAuthActivity.class);
                        getUiControlInterface().launchActivity(true, intent);
                    }
                }));
    }


    public void orderCreate() {
        //如果没有登录, 跳转登录并返回
        if (getUiControlInterface().handleNoLogin()) {
            return;
        }
        //query user authentication info.
        getUiControlInterface().showProgress(getString(R.string.checking_authen_info));
        UserManager.getInstance().getAuthInfo(new DataListener<UserAuthInfo>() {
            @Override
            public void onSuccess(UserAuthInfo data, String message) {
                if (data == null) {
                    showUserAuthHintDialog(false);
                } else {
                    if (data.getState() == AuthenticationInfoTo.Companion.getIN_CHECKING()) {
                        getUiControlInterface().showAlert(UIUtils.createHintDialog(
                                mContext,
                                getString(R.string.hint),
                                getString(R.string.in_auth_hint),
                                null));
                    } else if (data.getState() == AuthenticationInfoTo.Companion.getAUTH_FAILED()) {
                        showUserAuthHintDialog(true);
                    } else if (data.getState() == AuthenticationInfoTo.Companion.getAUTH_SUCCESS()) {
                        OrderManager.getInstance().setOrderHouse(mHouseDetail);
                        Intent intent = new Intent(mContext, ContractActivity.class);
                        getUiControlInterface().launchActivity(true, intent);
                    }
                }
            }

            @Override
            public void onFailed(Throwable error, String message) {
                getUiControlInterface().showToast(message);
            }

            @Override
            public void onEnd() {
                getUiControlInterface().dismissProgress();
            }
        });
    }
}
